package org.leyi.shop.util;

import jakarta.servlet.http.HttpServletRequest;
import org.leyi.shop.model.vo.SafetyUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static org.leyi.shop.common.Constant.REQUEST_SESSION_USER_KEY;

/**
 *
 * description:  对servlet的工具类
 * @author  lenovo
 * create 2024/5/6-21:52 */
public class HttpServletUtils {
    /**
     *
     * description: 获取request
     * @return HttpServletRequest
     * @author  lenovo
     * create 2024/5/6-21:53
     */
    public static HttpServletRequest getHttpServletRequest()
    {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     *
     * description: 获取request的Ip
     * @return String 返回ip
     * @author  lenovo
     * create 2024/5/6-21:55
     */
    public static String getIp()
    {
        return getHttpServletRequest().getRemoteUser();
    }

    /**
     *
     * description: 获得当前用户的登录态（使用前请用判断登录态是否存在)
     * @return SafetyUser
     * @author  lenovo
     * create 2024/5/9-15:18
     */
    public static SafetyUser getCurrentUser() {
        return (SafetyUser) getHttpServletRequest().getSession().getAttribute(REQUEST_SESSION_USER_KEY);
    }
}
